package com.useeinfo.oa.modules.inspection.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrderWorkContent;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("inspectionOrderWorkContentDao")
public class InspectionOrderWorkContentDao implements CrudDao<InspectionOrderWorkContent> {

    private final static Logger logger = LoggerFactory.getLogger(InspectionOrderWorkContentDao.class);

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);
        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String facilityInspectionSnapId = queryHash.get("facilityInspectionSnapId");
        if (StringUtils.isNotBlank(facilityInspectionSnapId)) {
            conditionHash.put("s.facilityInspectionSnap.id = ?{paramIndex} ", StringConverters.null2Long(facilityInspectionSnapId));
        }
        String inspectionOrderId = queryHash.get("inspectionOrderId");
        if (!StringUtils.isEmpty(inspectionOrderId)) {
            conditionHash.put("(s.facilityInspectionSnap.inspectionOrder.id = ?{paramIndex}) ", StringConverters.null2Long(inspectionOrderId));
        }
        String applicationId = queryHash.get("applicationId");
        if (!StringUtils.isEmpty(applicationId)) {
            conditionHash.put("(s.application_scenarios_id = ?{paramIndex}) ", StringConverters.null2Long(applicationId));
        }
        String distribute = queryHash.get("distribute");
        if (StringUtils.isNotBlank(distribute)) {
            conditionHash.put("s.distribute= ?{paramIndex} ", StringConverters.null2Boolean(distribute));
        }
        String orderId = queryHash.get("resultNotnull");
        if (!StringUtils.isEmpty(orderId)) {
            conditionHash.put("s.application_scenarios_name in (select application_scenarios_name from InspectionOrderWorkContent m where m.result is not null and m.result<>'' and m.deleteStatus=false and m.facilityInspectionSnap.id = ?{paramIndex} group by application_scenarios_name )", StringConverters.null2Long(orderId));
        }
        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from InspectionOrderWorkContent s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<InspectionOrderWorkContent> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from InspectionOrderWorkContent s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<InspectionOrderWorkContent> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, InspectionOrderWorkContent.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public InspectionOrderWorkContent findModel(Long id) {
        return entityManager.find(InspectionOrderWorkContent.class, id);
    }

    @Override
    public Integer add(InspectionOrderWorkContent model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("InspectionOrderWorkContentDaoImpl添加inspectionOrderWorkContent成功！");
        return 1;
    }

    @Override
    public Integer update(InspectionOrderWorkContent model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        InspectionOrderWorkContent inspectionOrderWorkContent = entityManager.find(InspectionOrderWorkContent.class, id);
        entityManager.remove(inspectionOrderWorkContent);
        return 1;
    }

    @Override
    public Integer deleteLogic(InspectionOrderWorkContent model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
